package org.fuys.coder.infrastructure.repository;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.fuys.coder.infrastructure.model.dto.LotteryStrategyDTO;
import org.fuys.coder.common.entity.Pair;
import org.fuys.coder.domain.act.lottery.model.vo.strategy.LotteryStrategyDetailVO;
import org.fuys.coder.domain.act.lottery.model.vo.strategy.LotteryStrategyVO;
import org.fuys.coder.domain.act.lottery.repository.ILotteryStrategyRepository;
import org.fuys.coder.common.entity.PageDTO;
import org.fuys.coder.infrastructure.dao.LotteryStrategyDetailMapper;
import org.fuys.coder.infrastructure.dao.LotteryStrategyMapper;
import org.fuys.coder.infrastructure.model.po.LotteryStrategy;
import org.fuys.coder.infrastructure.model.po.LotteryStrategyDetail;
import org.springframework.stereotype.Service;
import org.springframework.util.ObjectUtils;

import javax.annotation.Resource;
import java.util.List;
import java.util.stream.Collectors;

/**
 * @projectName: fuys-low-coder
 * @package: org.fuys.coder.infrastructure.repository
 * @className: LotteryStrategyRepository
 * @author: WangJing
 * @description: 抽奖策略仓储服务
 * @date: 2024/7/5 22:57
 * @version: 1.0
 */
@Service
public class LotteryStrategyRepository extends ServiceImpl<LotteryStrategyDetailMapper, LotteryStrategyDetail>
        implements ILotteryStrategyRepository {

    @Resource
    private LotteryStrategyMapper lotteryStrategyMapper;

    @Resource
    private LotteryStrategyDetailMapper lotteryStrategyDetailMapper;

    @Override
    public List<Pair<LotteryStrategyVO, LotteryStrategyDetailVO>> getAllStrategyInfo() {
        final List<LotteryStrategyDTO> allInfo = lotteryStrategyMapper.getAllInfo();
        if(!ObjectUtils.isEmpty(allInfo))
        return allInfo.stream().map(dto -> {
            Pair<LotteryStrategyVO, LotteryStrategyDetailVO> resultPair = new Pair<>();
            LotteryStrategyVO strategyVO = new LotteryStrategyVO();
            strategyVO.setSuitNum(dto.getSuitNum());
            strategyVO.setStrategyDesc(dto.getStrategyDesc());
            strategyVO.setStrategyMode(dto.getStrategyMode());
            strategyVO.setExtInfo(dto.getExtInfo());
            strategyVO.setId(dto.getId());
            LotteryStrategyDetailVO detailVO = new LotteryStrategyDetailVO();
            detailVO.setPosition(dto.getPosition());
            detailVO.setWeight(dto.getWeight());
            resultPair.setLeft(strategyVO);
            resultPair.setRight(detailVO);
            return resultPair;
        }).collect(Collectors.toList());
        return null;
    }

    @Override
    public Integer addStrategy(LotteryStrategyVO strategyVO) {
        LotteryStrategy lotteryStrategy=new LotteryStrategy();
        lotteryStrategy.setStrategyDesc(strategyVO.getStrategyDesc());
        lotteryStrategy.setStrategyMode(strategyVO.getStrategyMode());
        lotteryStrategy.setExtInfo(strategyVO.getExtInfo());
        lotteryStrategy.setSuitNum(strategyVO.getSuitNum());
        lotteryStrategyMapper.insert(lotteryStrategy);
        return lotteryStrategy.getId();
    }

    @Override
    public void addDetails(List<LotteryStrategyDetailVO> detailVOS) {
        List<LotteryStrategyDetail> lotteryStrategies = detailVOS.stream().map(detailVO -> {
            LotteryStrategyDetail lotteryStrategy = new LotteryStrategyDetail();
            lotteryStrategy.setStrategyId(detailVO.getStrategyId());
            lotteryStrategy.setPosition(detailVO.getPosition());
            lotteryStrategy.setWeight(detailVO.getWeight());
            return lotteryStrategy;
        }).collect(Collectors.toList());
        this.saveBatch(lotteryStrategies);
    }

    @Override
    public List<Pair<LotteryStrategyVO, LotteryStrategyDetailVO>> pageStrategyInfo(PageDTO pageDTO) {
        return null;
    }
}
